//
//  AppDelegate+third.m
//  MuYing
//
//  Created by tbb on 2020/2/27.
//  Copyright © 2020 TBB. All rights reserved.
//

#import "AppDelegate+third.h"
#import <AlipaySDK/AlipaySDK.h>
#import <FBSDKCoreKit/FBSDKCoreKit.h>
#import <UMCommon/UMConfigure.h>



@implementation AppDelegate (third)
-(void)thirdShare {
    [UMConfigure initWithAppkey:@"582acfbf8f4a9d0809000230" channel:@"App Store"];
//    [WXApi registerApp:@"wxa7b9130e953c0d4c"];
}


- (BOOL)application:(UIApplication *)application
            openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication
         annotation:(id)annotation {
    
    if ([url.host isEqualToString:@"safepay"]) {
        //跳转支付宝钱包进行支付，处理支付结果
        [[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
            NSLog(@"result = %@",resultDic);
            [self withAlipaySDKResultDic:resultDic];
        }];
    }
    return YES;
}


// NOTE: 9.0以后使用新API接口
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString*, id> *)options
{

    [[FBSDKApplicationDelegate sharedInstance] application:app openURL:url sourceApplication:options[UIApplicationOpenURLOptionsSourceApplicationKey] annotation:options[UIApplicationOpenURLOptionsAnnotationKey] ];
    //微信支付与微信登录
     if ([url.host isEqualToString:@"pay"]||[url.host isEqualToString:@"oauth"])
    {
        return [WXApi handleOpenURL:url delegate:self];
    }
   
    if ([url.host isEqualToString:@"safepay"]) {
        //跳转支付宝钱包进行支付，处理支付结果
        [[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
            NSLog(@"result = %@",resultDic);
            [self withAlipaySDKResultDic:resultDic];
        }];
    }
    return YES;
}
///处理支付宝，支付返回结果
-(void)withAlipaySDKResultDic:(NSDictionary *)resultDic {
    NSNotificationCenter *notiCenter=[NSNotificationCenter defaultCenter];
    if ([resultDic[@"resultStatus"] intValue] == 9000) {
        /*支付成功通知*/
        [notiCenter postNotificationName:YJPaySucceedNotiKey object:self userInfo:nil];
    }else{
        
        if ([resultDic[@"resultStatus"] intValue] == 6001) {
            /*用户取消了支付*/
            dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC));
            dispatch_after(delayTime, dispatch_get_main_queue(), ^{
                MBErrHUD(kYQLS(@"支付已取消"));
            });
        }else{
            NSString * strMsg = [NSString stringWithFormat:@"%@：%@,%@",kYQLS(@"支付结果"),kYQLS(@"支付失败"),resultDic[@"memo"]];
            //NSLog(@"strMsg: %@",strMsg);
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:strMsg delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [alert show];
        }
    }
}
@end
